# -*- coding: utf8 -*-

import urllib2
from puppy.model import Query, Response, Item

def search(query):
  """Search function for PuppyIR Pathfinder.
  
  Parameters:
  
  * query (puppy.model.OpenSearch.Query)
  
  Returns:
  
  * puppy.model.OpenSearch.Response
  
  Raises:
  
  * urllib2.URLError
  
  """
  page = 1
  format = 'rss'
  age = 'v'
  url = "http://pathfinder.cs.utwente.nl/cgi-bin/opensearch/ekz.cgi?query={0}&page={1}&format={2}&leeftijd={3}".format(urllib2.quote(query.search_terms), page, format, age)
  try:
    response = urllib2.urlopen(url)
    return Response.parse_xml(response.read())
  except urllib2.URLError, e:
    print "Error in Search Service: PuppyIR Pathfinder search failed"
